﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using System.Web.Security;
public partial class admin_Newreport : System.Web.UI.Page
{
	String surveydate;
	bool lastcomponent = false;
	int componentID;

	DataTable historyDT;
	int machineid;
	protected void Page_Load(object sender, EventArgs e)
	{

		String componentname = (String)Request.QueryString["componentname"];
		String machinename = (String)Request.QueryString["machinename"];
		machineid = Int32.Parse((String)Request.QueryString["machineid"]);
		componentID = Int32.Parse((String)Request.QueryString["componentID"]);
		lastcomponent = bool.Parse((string)Request.QueryString["lastcomponent"]);
		if (!lastcomponent)
		{
			DropDownList_Matrix.Visible = false;
			matrix_label.Visible = false;
			Matrix_Textbox.Visible = false;
		}
		surveydate = (String)Session["SurveyDate"];
		Pre_date.Text = "";
		if (!IsPostBack)
		{

			//       isMultifaults.Visible = false;



			componentLabel.Text = machinename + "------" + componentname;
			Comment cmt = new Comment();
			historyDT = cmt.getCurrentRecom(componentID, surveydate);
			if (historyDT.Rows.Count > 0)
			{
				foreach (DataRow dr_his in historyDT.Rows)
				{
					String historyDate = dr_his["dateRecorded1"].ToString();
					Pre_date.Text = historyDate;
					PreviousComment.Text = dr_his["comments"].ToString();
					break;
				}
			}
			else
			{
				PreviousComment.Text = "Not Avaiable";
			}
			this.addFaultlist(1, 0);
			this.addFaultlist(1, 1);
			this.addFaultlist(1, 2);
			cmt = new Comment();
			int fault1ID = Int32.Parse(Fault1_DropDownList.SelectedValue);
			int severity1 = Int32.Parse(Severity_DropDownList.SelectedValue);
			DataTable dt2 = cmt.getRecomm(fault1ID, severity1, 0, 0, 0, 0);
			if (dt2.Rows.Count > 0)
			{
				foreach (DataRow dr in dt2.Rows)//Note, the datatable name should be consistent
				{
					String recomm = dr["recommendation"].ToString();
					string RecommenID = dr["recommend_id"].ToString();
					if (recomm.Length > 45)
					{
						recomm = recomm.Substring(0, 45) + "...";
					}
					ListItem RecommItem = new ListItem(recomm, RecommenID, true);
					Reco_DropDownList.Items.Add(RecommItem);
					Reco_DropDownList0.Items.Add(RecommItem);
				}
			}
			cmt = new Comment();
			DataTable dt3 = cmt.getTemplate();
			foreach (DataRow dr in dt3.Rows)
			{
				String templatecomment = dr["commentDescription"].ToString();
				string templateID = dr["comment_id"].ToString();
				ListItem templateitem = new ListItem(templatecomment, templateID, true);
			}
			cmt = new Comment();
			DataTable dtResury = cmt.getResurvey();
			foreach (DataRow dr in dtResury.Rows)
			{
				String resuryid = dr["resurvey_id"].ToString();
				string resurydes = dr["resurvey_des"].ToString();
				ListItem resurveyitem = new ListItem(resurydes, resuryid, true);
				Resurvey_DropDownList.Items.Add(resurveyitem);
			}
		}
	}
	public void addFaultlist(int faulttype, int FaultListID)
	{
		if (FaultListID == 0)
		{
			Fault1_DropDownList.Items.Clear();
		}
		else if (FaultListID == 1)
		{
			Fault1_DropDownList0.Items.Clear();
		}
		else
		{
			Fault3_DropDownList.Items.Clear();
		}


		Comment cmt = new Comment();
		DataTable dt = cmt.getFaults(componentID);
		foreach (DataRow dr in dt.Rows)
		{
			string faultid = dr["fault_id"].ToString();
			String faultname = dr["faultName"].ToString();
			string faultDes = dr["faultDes"].ToString();
			if (faultDes.Equals(""))
				continue;
			if (faultDes.Length > 45)
			{
				faultDes = faultDes.Substring(0, 45) + "...";
			}
			ListItem clienitem = new ListItem(faultDes, faultid, true);

			if (FaultListID == 0)
			{
				Fault1_DropDownList.Items.Add(clienitem);
			}
			else if (FaultListID == 1)
			{
				Fault1_DropDownList0.Items.Add(clienitem);
			}
			else
			{
				Fault3_DropDownList.Items.Add(clienitem);
			}

		}
	}

	public void fault1changed(object sender, EventArgs e)
	{
		Reco_DropDownList.Items.Clear();
		int severity1ID = Int32.Parse(Severity_DropDownList.SelectedValue);
		int fault1id = Int32.Parse(Fault1_DropDownList.SelectedItem.Value);
		Comment ct = new Comment();
		DataTable dt = ct.getRecomm(fault1id, severity1ID, 0, 0, 0, 0);
		if (dt.Rows.Count > 0)
		{
			foreach (DataRow dr in dt.Rows)//Note, the datatable name should be consistent
			{
				String recomm = dr["recommendation"].ToString();
				string RecommenID = dr["recommend_id"].ToString();
				if (recomm.Length > 45)
				{
					recomm = recomm.Substring(0, 45) + "...";
				}
				ListItem RecommItem = new ListItem(recomm, RecommenID, true);

				Reco_DropDownList.Items.Add(RecommItem);
				//  Reco_DropDownList0.Items.Add(RecommItem);
			}
		}
	}
	public void fault2changed(object sender, EventArgs e)
	{
		Reco_DropDownList.Items.Clear();
		Reco_DropDownList0.Items.Clear();
		int fault1id = Int32.Parse(Fault1_DropDownList.SelectedItem.Value);
		int severity1ID = Int32.Parse(Severity_DropDownList.SelectedValue);

		int severity2ID = Int32.Parse(Severity_DropDownList0.SelectedValue);
		int fault2id = Int32.Parse(Fault1_DropDownList0.SelectedItem.Value);
		Comment ct = new Comment();
		DataTable dt = ct.getRecomm(fault1id, severity1ID, fault2id, severity2ID, 0, 0);
		if (dt != null)
		{
			foreach (DataRow dr in dt.Rows)//Note, the datatable name should be consistent
			{
				String recomm = dr["recommendation"].ToString();
				string RecommenID = dr["recommend_id"].ToString();
				if (recomm.Length > 45)
				{
					recomm = recomm.Substring(0, 45) + "...";
				}
				ListItem RecommItem = new ListItem(recomm, RecommenID, true);
				Reco_DropDownList0.Items.Add(RecommItem);
			}
		}
	}
	public void fault3changed(object sender, EventArgs e)
	{
		Reco_DropDownList.Items.Clear();
		Reco_DropDownList0.Items.Clear();
		Reco3_DropDownList.Items.Clear();
		int fault1id = Int32.Parse(Fault1_DropDownList.SelectedItem.Value);
		int severity1ID = Int32.Parse(Severity_DropDownList.SelectedValue);

		int severity2ID = Int32.Parse(Severity_DropDownList0.SelectedValue);
		int fault2id = Int32.Parse(Fault1_DropDownList0.SelectedItem.Value);

		int severity3ID = Int32.Parse(Severity3_DropDownList.SelectedValue);
		int fault3id = Int32.Parse(Fault3_DropDownList.SelectedItem.Value);
		Comment ct = new Comment();
		DataTable dt = ct.getRecomm(fault1id, severity1ID, fault2id, severity2ID, fault3id, severity3ID);

		if (dt.Rows.Count > 0)
		{
			foreach (DataRow dr in dt.Rows)//Note, the datatable name should be consistent
			{
				String recomm = dr["recommendation"].ToString();
				string RecommenID = dr["recommend_id"].ToString();
				if (recomm.Length > 45)
				{
					recomm = recomm.Substring(0, 45) + "...";
				}
				ListItem RecommItem = new ListItem(recomm, RecommenID, true);
				Reco3_DropDownList.Items.Add(RecommItem);
			}
		}
	}
	public void severityChanged(object sender, EventArgs e)
	{
		Reco_DropDownList.Items.Clear();
		int severityID = Int32.Parse(Severity_DropDownList.SelectedValue);
		int faultid = Int32.Parse(Fault1_DropDownList.SelectedItem.Value);
		Comment ct = new Comment();
		DataTable dt = ct.getRecomm(faultid, severityID, 0, 0, 0, 0);
		if (dt.Rows.Count > 0)
		{
			foreach (DataRow dr in dt.Rows)//Note, the datatable name should be consistent
			{
				String recomm = dr["recommendation"].ToString();
				string RecommenID = dr["recommend_id"].ToString();
				ListItem RecommItem = new ListItem(recomm, RecommenID, true);
				Reco_DropDownList.Items.Add(RecommItem);
				//  Reco_DropDownList0.Items.Add(RecommItem);
			}
		}

	}

	public void severityChanged0(object sender, EventArgs e)
	{
		Reco_DropDownList.Items.Clear();

		Reco_DropDownList0.Items.Clear();
		int severity2ID = Int32.Parse(Severity_DropDownList0.SelectedValue);
		int fault2id = Int32.Parse(Fault1_DropDownList0.SelectedItem.Value);
		int severity1ID = Int32.Parse(Severity_DropDownList.SelectedValue);
		int fault1id = Int32.Parse(Fault1_DropDownList.SelectedItem.Value);

		Comment ct = new Comment();
		DataTable dt = ct.getRecomm(fault1id, severity1ID, fault2id, severity2ID, 0, 0);

		if (dt != null)
		{
			foreach (DataRow dr in dt.Rows)//Note, the datatable name should be consistent
			{
				String recomm = dr["recommendation"].ToString();
				string RecommenID = dr["recommend_id"].ToString();
				ListItem RecommItem = new ListItem(recomm, RecommenID, true);
				Reco_DropDownList0.Items.Add(RecommItem);
				//  Reco_DropDownList0.Items.Add(RecommItem);
			}
		}
	}
	public void severity3Changed(object sender, EventArgs e)
	{
		Reco_DropDownList.Items.Clear();

		Reco_DropDownList0.Items.Clear();
		Reco3_DropDownList.Items.Clear();
		int severity1ID = Int32.Parse(Severity_DropDownList.SelectedValue);
		int fault1id = Int32.Parse(Fault1_DropDownList.SelectedItem.Value);
		int severity2ID = Int32.Parse(Severity_DropDownList0.SelectedValue);
		int fault2id = Int32.Parse(Fault1_DropDownList0.SelectedItem.Value);

		int severity3ID = Int32.Parse(Severity3_DropDownList.SelectedValue);
		int fault3id = Int32.Parse(Fault3_DropDownList.SelectedItem.Value);

		Comment ct = new Comment();

		DataTable dt = ct.getRecomm(fault1id, severity1ID, fault2id, severity2ID, fault3id, severity3ID);
		if (dt.Rows.Count > 0)
		{
			foreach (DataRow dr in dt.Rows)//Note, the datatable name should be consistent
			{
				String recomm = dr["recommendation"].ToString();
				string RecommenID = dr["recommend_id"].ToString();
				ListItem RecommItem = new ListItem(recomm, RecommenID, true);
				Reco3_DropDownList.Items.Add(RecommItem);
				//  Reco_DropDownList0.Items.Add(RecommItem);
			}
		}
	}
	protected void Generate_Button_Click(object sender, EventArgs e)
	{
		if (CheckBox_oldreport.Checked)
		{
			Comment_tb.Text = PreviousComment.Text;
			Comment_tb.ReadOnly = false;
			//PreviousComment.ReadOnly = false;

		}
		else
		{
			//PreviousComment.ReadOnly = false;
			//PreviousComment.ReadOnly = true;
			string severity = Severity_DropDownList.SelectedValue;
			string fault1sev = Severity_DropDownList.SelectedItem.Text;
			string severity0 = "";
			string severityID = "";
			string Twofaults = "";
			string threefaults = "";
			string faultscount = isMultifaults.Text;
			if (isMultifaults.Text.Equals("0"))
			{

				String newreport = "indicates " + fault1sev + " " + Fault1_DropDownList.SelectedItem.Text + " " + Reco_DropDownList.SelectedItem.Text + " " +
					Resurvey_DropDownList.SelectedItem.Text;

				Comment_tb.Text = newreport;
			}
			else if (isMultifaults.Text.Equals("1"))
			{
				int fault1ID = int.Parse(Fault1_DropDownList.SelectedValue);
				int fault2ID = int.Parse(Fault1_DropDownList0.SelectedValue);
				string faultcondition = getCombinedComment(fault1ID, fault2ID, 0);
				Twofaults = faultcondition + " " + Reco_DropDownList0.SelectedItem.Text;
				severity0 = Severity_DropDownList0.SelectedItem.Text;

				severityID = Severity_DropDownList0.SelectedValue;
				//Twofaults = fault1sev + " " + Fault1_DropDownList.SelectedItem.Text + "  " + Reco_DropDownList.SelectedItem.Text +
				//   ";  " + severity0 + " " + Fault1_DropDownList0.SelectedItem.Text + "  " + Reco_DropDownList0.SelectedItem.Text + " ";
				Comment_tb.Text = Twofaults + Resurvey_DropDownList.SelectedItem.Text;
				//componentSeverity = Int32.Parse(severity) >Int32.Parse( severity0)?Int32.Parse(severity):Int32.Parse(severity0);
			}
			else
			{
				int fault1ID = int.Parse(Fault1_DropDownList.SelectedValue);
				int fault2ID = int.Parse(Fault1_DropDownList0.SelectedValue);
				int fault3ID = int.Parse(Fault3_DropDownList.SelectedValue);

				string severity3 = Severity3_DropDownList.SelectedItem.Text;
				string severity3ID = Severity3_DropDownList.SelectedValue;
				threefaults = this.getCombinedComment(fault1ID, fault2ID, fault3ID) +
				Reco3_DropDownList.SelectedItem.Text + Resurvey_DropDownList.SelectedItem.Text;
				Comment_tb.Text = threefaults;
				//int sev1 = Int32.Parse(fault1sev);
				//int sev2 = Int32.Parse(severityID);
				//int sev3 = Int32.Parse(severity3ID);
				//componentSeverity = (sev1<sev2)?(sev2<sev3?sev3:sev2):(sev1<sev3?sev3:sev1);
			}


		}

	}
	protected string getCombinedComment(int fault1ID, int fault2ID, int fault3ID)
	{//indicates faultseerity1 + (both) +faultcondition1+(and)+severity2+faultcondition2+(and)+severity+faultcondition3;
		string str = "";
		string fault1, fault2, fault3;
		string severity1, severity2, severity3;
		fault1 = Fault1_DropDownList.SelectedItem.Text;
		severity1 = Severity_DropDownList.SelectedItem.Text;
		if (fault3ID == 0)
		{
			severity2 = Severity_DropDownList0.SelectedItem.Text;
			fault2 = Fault1_DropDownList0.SelectedItem.Text;
			str = "indicates " + " both " + severity1 + fault1 + " and " + severity2 + " " + fault2;
		}
		else
		{
			severity2 = Severity_DropDownList0.SelectedItem.Text;
			fault2 = Fault1_DropDownList0.SelectedItem.Text;
			fault3 = Fault3_DropDownList.SelectedItem.Text;
			severity3 = Severity_DropDownList0.SelectedItem.Text;
			str = "indicates " + severity1 + " " + fault1 + " , " + severity2 + " " + fault2 + " and " + severity3 + " " + fault3;


		}
		return str;
	}
	protected void submit_button_Click(object sender, EventArgs e)//insert new report
	{
		if (!Page.IsValid)
		{
		}
		else
		{
			if (CheckBox_oldreport.Checked)
			{
				Comment cmt = new Comment();
				historyDT = cmt.getCurrentRecom(componentID, surveydate);
				foreach (DataRow dr in historyDT.Rows)
				{
					int RC1 = 0;
					int RC2 = 0, RC3 = 0;
					if (!dr.IsNull("recommendID_1"))
					{
						RC1 = Int32.Parse(dr["recommendID_1"].ToString());
					}
					if (dr.IsNull("recommendID_2"))
					{
					}
					else if (dr.IsNull("recommendID_3"))
					{
						RC2 = Int32.Parse(dr["recommendID_2"].ToString());
					}
					else
					{
						RC2 = Int32.Parse(dr["recommendID_2"].ToString());
						RC3 = Int32.Parse(dr["recommendID_3"].ToString());

					}
					if (!dr.IsNull("resurvey_id"))
					{
						int resurvyID = Int32.Parse(dr["resurvey_id"].ToString());
						int severityID = Int32.Parse(dr["status_id"].ToString());
						string comments = dr["comments"].ToString();
						string comments2 = Comment_tb.Text;
						if (!comments2.Equals(""))
						{
							comments = comments2;
						}
						cmt = new Comment();
						cmt.insertNewreport(componentID, severityID, comments, surveydate, RC1, RC2, RC3, resurvyID);

					}
					else
					{
						int severityID = Int32.Parse(dr["status_id"].ToString());
						string comments = dr["comments"].ToString();
						cmt = new Comment();
						cmt.insertNewreport(componentID, severityID, comments, surveydate, RC1, RC2, RC3, 0);


					}
				}
			}
			else
			{
				int Recom1_id = 0, Recom2_id = 0, Recom3_id = 0;
				int severityID = Int32.Parse(Severity_DropDownList.SelectedValue);
				int severity2ID = 0, severity3ID = 0;
				int resurvyID = Int32.Parse(Resurvey_DropDownList.SelectedItem.Value);

				switch (Int32.Parse(isMultifaults.Text))
				{
					case 2:
						severity3ID = Int32.Parse(Severity3_DropDownList.SelectedValue);
						severity2ID = Int32.Parse(Severity_DropDownList0.SelectedValue);
						Recom3_id = Int32.Parse(Reco3_DropDownList.SelectedValue);
						break;
					case 1:
						severity2ID = Int32.Parse(Severity_DropDownList0.SelectedValue);
						Recom2_id = Int32.Parse(Reco_DropDownList0.SelectedValue);
						break;
					case 0: Recom1_id = Int32.Parse(Reco_DropDownList.SelectedValue); break;
				}
				Comment ct = new Comment();
				severityID = getSeverity(severityID, severity2ID, severity3ID);
				bool result = ct.insertNewreport(componentID, severityID, Comment_tb.Text, surveydate, Recom1_id, Recom2_id, Recom3_id, resurvyID);
				isMultifaults.Text = "0";
				if (lastcomponent)
				{
					ct = new Comment();
					int matrix = Int32.Parse(Matrix_Textbox.Text);
					ct.InsertMatrix(surveydate, machineid, matrix);
				}
				if (result)
				{
					Response.Write("<script language=javascript> alert('success')</script>");
				}
				else
				{
					Response.Write("<script language=javascript> alert('failed')</script>");
				}
			}

		}
	}
	public int getSeverity(int id1, int id2, int id3)
	{
		int severity = 0;
		if (id2 == 0)
		{//only one fault chosen
			severity = id1;
		}
		else if (id3 == 0)
		{//two faults chosen
			severity = id1 > id2 ? id1 : id2;
		}
		else
		{//three faults chosen;
			severity = id1 > id2 ? (id1 > id3 ? id1 : id3) : (id2 > id3 ? id2 : id3);
		}
		return severity;
	}
	protected void ServerValidate(object source, ServerValidateEventArgs args)
	{
		int input;
		int severity = Int32.Parse(DropDownList_Matrix.SelectedValue);
		bool parseOk = int.TryParse(args.Value, out input);
		switch (severity)
		{

			case 1: args.IsValid = parseOk && (input > 0 && input <= 75); break;
			case 2: args.IsValid = parseOk && (input > 75 && input <= 100); break;
			case 3: args.IsValid = parseOk && (input > 100 && input <= 125); break;
			case 4: args.IsValid = parseOk && (input > 125 && input <= 150); break;
			case 5: args.IsValid = parseOk && (input > 0 && input <= 150); break;

		}
	}
	protected void LoginStatus1_LoggingOut(object sender, LoginCancelEventArgs e)
	{


		Session.Contents.Remove("UserDetails");
		Session.Abandon();
		Session.Contents.RemoveAll();
		FormsAuthentication.SignOut();
		Response.Redirect("../Default.aspx");
	}
}
